﻿using FileZillaConfig.Helpers;
using FileZillaConfig.Types;
using System;
using System.Collections.Generic;
using System.Xml.Linq;
namespace FileZillaConfig.ConfigClasses
{
    public class SpeedLimits
    {
        private SpeedLimitType _downloadType;
        private SpeedLimitType _uploadType;
        private int _downloadLimit = 10;
        private int _uploadLimit = 10;
        private List<LimitCriteria> _downloadLimitRules;
        private List<LimitCriteria> _uploadLimitRules;

        #region PROPERTIES
        /// <summary>
        /// Upload limit rules
        /// </summary>
        public List<LimitCriteria> UploadLimitRules
        {
            get { return _uploadLimitRules; }
            set { _uploadLimitRules = value; }
        }


        /// <summary>
        /// Download limit rules
        /// </summary>
        public List<LimitCriteria> DownloadLimitRules
        {
            get { return _downloadLimitRules; }
            set { _downloadLimitRules = value; }
        }


        /// <summary>
        /// Constant upload limit in kB/s
        /// </summary>
        public int UploadLimit
        {
            get { return _uploadLimit; }
            set { _uploadLimit = value; }
        }


        /// <summary>
        /// Constant download limit in kB/s
        /// </summary>
        public int DownloadLimit
        {
            get { return _downloadLimit; }
            set { _downloadLimit = value; }
        }


        /// <summary>
        /// Upload limit type
        /// </summary>
        public SpeedLimitType UploadType
        {
            get { return _uploadType; }
            set { _uploadType = value; }
        }


        /// <summary>
        /// Download limit type
        /// </summary>
        public SpeedLimitType DownloadType
        {
            get { return _downloadType; }
            set { _downloadType = value; }
        } 
        #endregion

        public SpeedLimits()
        {
            this._uploadLimitRules = new List<LimitCriteria>();
            this._downloadLimitRules = new List<LimitCriteria>();
            this._downloadType = SpeedLimitType.NoLimit;
            this._uploadType = SpeedLimitType.NoLimit;
        }

        public XElement getXML()
        {
            return new XElement("SpeedLimits",
                this._getRules("Download", this._downloadLimitRules),
                this._getRules("Upload", this._uploadLimitRules)
                );
        }

        private XElement _getRules(string name, List<LimitCriteria> list)
        {
            XElement container = new XElement(name);

            foreach (LimitCriteria lRules in list)
            {
                XElement Rule = new XElement("Rule");
                Rule.Add(new XAttribute("Speed", lRules.Speed.ToString()));
                Rule.Add(new XElement("Days", Convert.ToInt32(lRules.WeekDays).ToString()));
                if (Check.Date(lRules.Date))
                {
                    Rule.Add(new XElement("Date",
                                    new XAttribute("Year", lRules.Date.Year.ToString()),
                                    new XAttribute("Month", lRules.Date.Month.ToString()),
                                    new XAttribute("Day", lRules.Date.Day.ToString())));
                }

                if (Check.Time(lRules.FromTime))
                {
                    Rule.Add(new XElement("From",
                                    new XAttribute("Hour", lRules.FromTime.Hours.ToString()),
                                    new XAttribute("Minute", lRules.FromTime.Minutes.ToString()),
                                    new XAttribute("Second", lRules.FromTime.Seconds.ToString())));
                }

                if (Check.Time(lRules.ToTime))
                {
                    Rule.Add(new XElement("To",
                                    new XAttribute("Hour", lRules.ToTime.Hours.ToString()),
                                    new XAttribute("Minute", lRules.ToTime.Minutes.ToString()),
                                    new XAttribute("Second", lRules.ToTime.Seconds.ToString())));
                }

                container.Add(Rule);
            }

            return container;
        }

        

    }
}
